#include "stdafx.h"
#include "CameraHelper.h"
#include "Components\CameraComponent.h"

using namespace ZeEngine;

CameraComponent* CameraHelper::m_pTopCamera = NULL;
CameraComponent* CameraHelper::m_pBottomCamera = NULL;

CameraHelper::CameraHelper()
{
}


CameraHelper::~CameraHelper()
{
}

Vector2D<s32> CameraHelper::GetLogicalXYPosition(const Vector2D<s32>& position, const Vector2D<u32>& logicalResolution)
{
    Vector2D<s32> adjustedPosition;

    //We have 2 screens in gualem the top and bottom, so depending where we click we need to add an offset
    auto yOffset = logicalResolution.GetY() / 4;

    if (position.GetY() >= static_cast<s32>(yOffset))
    {
        adjustedPosition = position - Vector2D<s32>(0, yOffset);
    }

    return adjustedPosition;
}

Vector2D<s32> CameraHelper::AbsoluteToLogicalPosition(const Vector2D<s32>& position, const Vector2D<u32>& logicalResolution)
{
    Vector2D<s32> adjustedPosition;

    Vector2D<s32> cameraOffset = IsOnTop(position, logicalResolution) ? m_pTopCamera->GetPosition() :  m_pBottomCamera->GetPosition();

    //We have 2 screens in gualem the top and bottom, so depending where we click we need to add an offset
    auto yOffset = logicalResolution.GetY() / 4;

    if (position.GetY() >= static_cast<s32>(yOffset))
    {
        adjustedPosition = position - Vector2D<s32>(0, yOffset);
    }

    return adjustedPosition + cameraOffset;
}

Vector2D<s32> CameraHelper::LogicalToAbsolutePosition(const Vector2D<s32>& position, const Vector2D<u32>& logicalResolution, bool fTop)
{
    Vector2D<s32> adjustedPosition;

    Vector2D<s32> cameraOffset = IsOnTop(position, logicalResolution) ? m_pTopCamera->GetPosition() :  m_pBottomCamera->GetPosition();

    //We have 2 screens in gualem the top and bottom, so depending where we click we need to add an offset
    auto yOffset = logicalResolution.GetY() / 4;

    if (!fTop)
    {
        adjustedPosition = position + Vector2D<s32>(0, yOffset);
    }

    return adjustedPosition - cameraOffset;
}

bool CameraHelper::IsOnTop(const Vector2D<s32>& position, const Vector2D<u32>& logicalResolution)
{
    bool clickedOnTop = true;
    if (position.GetY() > (static_cast<s32>(logicalResolution.GetY() / 4)))
    {
        clickedOnTop = false;
    }

    return clickedOnTop;
}

void CameraHelper::SetCameras(CameraComponent* pTopCamera, CameraComponent* pBottomCamera)
{
    m_pTopCamera = pTopCamera;
    m_pBottomCamera = pBottomCamera;
}